package cn.stronglink.assetmanage.business.check.checkorder;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import cn.stronglink.assetmanage.BaseActivity;
import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.check.add.AddNewCheckActivity;
import cn.stronglink.assetmanage.entity.Check;
import cn.stronglink.assetmanage.entity.MessageEvent;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.assetmanage.utils.dao.CheckDataUtil;

public class CheckActivity extends BaseActivity implements View.OnClickListener {
    TabLayout tbCheck;
    ViewPager vpCheck;
    private CheckViewPagerAdapter mAdapter;
    private FragmentManager fm;
    private String[] mCheckTitle = MyApplication.APP_CONTEXT.getResources().
            getStringArray(R.array.check);

    private Button btnAddCheck;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check);
        initView();
        initData();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        setSelectPosition(1);
        initData();
    }

    private void initView() {
        tbCheck = findViewById(R.id.tl_check);
        vpCheck = findViewById(R.id.vp_check);
        btnAddCheck = findViewById(R.id.btn_add_check);
        btnAddCheck.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initData() {
        fm = getSupportFragmentManager();
        List<Check> mCheckList = CheckDataUtil.getCheckedOrderNoDistince();
        List<Check> mUnCheckList = CheckDataUtil.getunCheckOrderNoDistince();
        int unCheckSize = mUnCheckList == null ? 0 : mUnCheckList.size();
        int checkSize = mCheckList == null ? 0 : mCheckList.size();
        mCheckTitle = new String[]{"待入库(" + unCheckSize + ")",
                "已入库(" + checkSize + ")"};
        mAdapter = new CheckViewPagerAdapter(fm, mCheckTitle);
        vpCheck.setAdapter(mAdapter);
        tbCheck.setupWithViewPager(vpCheck);
        tbCheck.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                vpCheck.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.btn_add_check:
                //新加入库单
                startActivity(new Intent(CheckActivity.this, AddNewCheckActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUpdateCheckData(MessageEvent messageEvent) {
        if (messageEvent!=null&& Constants.UPDATE_CHECK_DATA.equals(messageEvent.getType())){
            initData();
            setSelectPosition(1);
        }
    }


}
